﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxClasses;
using DevExpress.Web.ASPxEditors;
using DevExpress.Web.ASPxGridView;

namespace Project_Udrinkwater.PagesAdmin
{
    public partial class StockList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Session["Stock_ID"] = Session["sStock_ID"];
            ShowdataHead();
         
        }

        protected void ASPxGridView2_BeforePerformDataSelect(object sender, EventArgs e)
        {
            Session["Stock_ID"] = Session["sStock_ID"];


        }



        protected void ASPxGridView2_CustomColumnDisplayText(object sender, ASPxGridViewColumnDisplayTextEventArgs e)
        {

        }

        protected void ASPxGridView2_CellEditorInitialize(object sender, ASPxGridViewEditorEventArgs e)
        {
            //ASPxGridView grid = (ASPxGridView)sender;
            //switch (e.Column.FieldName)
            //{
            //    case "Product_Category_ID":
            //        InitializeCombo(e, "Product_ID", sqlProcate, cmbCombo2_OnCallback, grid);
            //        break;

            //    default:
            //        break;
            //}


            if (!ASPxGridView2.IsEditing || e.Column.FieldName != "Product_Category_ID") return;
            if (e.KeyValue == DBNull.Value || e.KeyValue == null) return;
            object val = ASPxGridView2.GetRowValuesByKeyValue(e.KeyValue, "Product_ID");
            if (val == DBNull.Value) return;
            string country = (string)val;

            ASPxComboBox combo = e.Editor as ASPxComboBox;
            FillCityCombo(combo, country);

            combo.Callback += new CallbackEventHandlerBase(cmbCity_OnCallback);

        }
        
        //cascade2
        protected void FillCityCombo(ASPxComboBox cmb, string country)
        {
            if (string.IsNullOrEmpty(country)) return;

            List<string> cities = GetCities(country);
            cmb.Items.Clear();
            foreach (string city in cities)
                cmb.Items.Add(city);
        }
        List<string> GetCities(string country)
        {
            List<string> list = new List<string>();
            sqlProcate.SelectParameters[0].DefaultValue = country;
            DataView view = (DataView)sqlProcate.Select(DataSourceSelectArguments.Empty);
            for (int i = 0; i < view.Count; i++)
            {
                list.Add((string)view[i][0]);
            }
            return list;
        }

        void cmbCity_OnCallback(object source, CallbackEventArgsBase e)
        {
            FillCityCombo(source as ASPxComboBox, e.Parameter);
        }

        //private void cmbCombo2_OnCallback(object source, CallbackEventArgsBase e)
        //{
        //    FillCombo(source as ASPxComboBox, e.Parameter, sqlProcate);
        //}




        //protected void InitializeCombo(ASPxGridViewEditorEventArgs e, string parentComboName, SqlDataSource source, CallbackEventHandlerBase callBackHandler, ASPxGridView grid)
        //{

        //    string id = string.Empty;
        //    if (!grid.IsNewRowEditing)
        //    {
        //        object val = grid.GetRowValuesByKeyValue(e.KeyValue, parentComboName);
        //        id = (val == null || val == DBNull.Value) ? null : val.ToString();
        //    }
        //    ASPxComboBox combo = e.Editor as ASPxComboBox;
        //    if (combo != null)
        //    {
        //        // unbind combo
        //        combo.DataSourceID = null;
        //        FillCombo(combo, id, source);
        //        combo.Callback += callBackHandler;
        //    }
        //    return;
        //}

        //protected void FillCombo(ASPxComboBox cmb, string id, SqlDataSource source)
        //{
        //    cmb.Items.Clear();
        //    // trap null selection
        //    if (string.IsNullOrEmpty(id)) return;

        //    // get the values
        //    source.SelectParameters[0].DefaultValue = id;
        //    DataView view = (DataView)source.Select(DataSourceSelectArguments.Empty);
        //    foreach (DataRowView row in view)
        //    {
        //        cmb.Items.Add(row[1].ToString(), row[0]);
        //    }
        //}




        private DataTable Validater()
        {
            string Instock = "";
            DataTable sa = new DataTable();
            DataSourceSelectArguments args = new DataSourceSelectArguments();
            DataView dvSql = (DataView)sqlValStock.Select(DataSourceSelectArguments.Empty);
            sa = dvSql.ToTable();

            return sa;


        }

        private void ShowdataHead()
        {
            DataTable dt = new DataTable();
            DataTable ds = new DataTable();
            DataView dvSql = (DataView) SqlDataSource1.Select(DataSourceSelectArguments.Empty);
            dt = dvSql.ToTable();
            Session["sEmp_ID"] = dt.Rows[0]["Act_Employee_ID"].ToString();
            txtDocNo.Text = dt.Rows[0]["Stock_doc_NO"].ToString();
            txtDateDoc.Text = dt.Rows[0]["Stock_Date"].ToString();
            DataView dvSql2 = (DataView)sdsEmp.Select(DataSourceSelectArguments.Empty);
            ds = dvSql2.ToTable();
            cmbEmp.Value = ds.Rows[0]["Employee_ID"].ToString();
        }

        protected void sqlValStock_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
        {
            e.Command.Parameters["@Product_ID"].Value = Session["ssProductID"].ToString();
        }

        protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
        {
            e.Command.Parameters["@Stock_ID"].Value = Session["sStock_ID"].ToString();
        }

        protected void sdsEmp_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
        {
            e.Command.Parameters["@Employee_ID"].Value = Session["sEmp_ID"].ToString();
        }

        protected void ASPxGridView2_RowValidating(object sender, DevExpress.Web.Data.ASPxDataValidationEventArgs e)
        {
            int Instock = 50;
            foreach (GridViewColumn column in ASPxGridView2.Columns)
            {
                GridViewDataColumn dataColumn = column as GridViewDataColumn;
                if (dataColumn == null) continue;
                string NameColumn = column.ToString();
                if (e.NewValues[dataColumn.FieldName] == null)
                {

                    if (!(NameColumn == "id" || NameColumn == "Stock_ID" || NameColumn == "Stock_doc_NO" || NameColumn == "ลำดับ"))
                    {
                        e.Errors[dataColumn] = "กรุณากรอกข้อมูลให้ครบ";
                        e.RowError = "กรอกข้อมูลให้สมบุูรณ์";
                    }

                }
                else
                {
                    string PIDFormGv = "";
                    Session["ssProductID"] = null;
                    PIDFormGv = e.NewValues["Product_ID"].ToString();
                    Session["ssProductID"] = PIDFormGv;
                    DataTable dataTable = Validater();
                    Instock = Convert.ToInt32(dataTable.Rows[0]["In_Stock"].ToString());

                }


                if (Instock <= 20 || (Convert.ToInt32(e.NewValues["Amount"]) == 0))
                {
                    if (Convert.ToInt32(e.NewValues["Transfer_Category_ID"]) == 1)
                    {
                        AddError(e.Errors, ASPxGridView2.Columns["Product_ID"], "สินค้าไม่เพียงพอ.");
                        e.RowError = "สินค้าไม่เพียงพอ";
                    }

                }


            }
           
           
            
            
        }
        
        private void AddError(Dictionary<GridViewColumn, string> errors, GridViewColumn column, string errorText)
        {
            if (errors.ContainsKey(column)) return;
            errors[column] = errorText;
        }

        protected void ASPxGridView2_StartRowEditing(object sender, DevExpress.Web.Data.ASPxStartRowEditingEventArgs e)
        {
            if (!ASPxGridView2.IsNewRowEditing)
                ASPxGridView2.DoRowValidation();
            
        }

        protected void ASPxGridView2_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
        {
         
        }

        protected void ASPxButton1_Click(object sender, EventArgs e)
        {
            Session["DocDate"] = txtDateDoc.Text;
            Response.Redirect("StockReport.aspx");
            
        }

        

        
    }
}